package com.sanpu.kxe.common.view;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.business.main.interfaces.IOnPromoteClickListener;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.utils.ScreenUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Description：
 * Created by 薛勇军 on 2018/8/20.
 */

public class MaterialImageView extends LinearLayout implements View.OnClickListener  {

    private Context mContext;

    private int mMaxSingleRowMenuSize = 3;//单行最大菜单数量
    private IOnPromoteClickListener mPromoteClickListener;
    private int mViewGroupWidth;

    public MaterialImageView(Context context) {
        super(context);
        init(context);
    }

    public MaterialImageView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MaterialImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 设置单行菜单数量
     *
     * @param maxSingleRowMenuSize 单行最大菜单数量
     */
    public void setMaxSingleRowMenuSize(int maxSingleRowMenuSize) {
        this.mMaxSingleRowMenuSize = maxSingleRowMenuSize;
    }

    private void init(Context context) {
        this.mContext = context;
        setOrientation(VERTICAL);
        try {
            WindowManager manager = ((Activity)mContext).getWindowManager();
            DisplayMetrics outMetrics = new DisplayMetrics();
            manager.getDefaultDisplay().getMetrics(outMetrics);
            mViewGroupWidth = outMetrics.widthPixels;
            mViewGroupWidth = (mViewGroupWidth- ScreenUtil.dp2px(110))/3;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建菜单
     *
     * @param ms                  List<DiscoveryMenuLayoutBean>
     * @param mPromoteClickListener 下载图片的控件
     */
    public void createMenu(List<String> ms, IOnPromoteClickListener mPromoteClickListener) {
        this.mPromoteClickListener = mPromoteClickListener;
        this.removeAllViews();
        int len = ms.size();

        LinearLayout rowView = null;
        for (int i = 0; i < len; i++) {
            if (i>5) break;
            if (rowView == null || i % mMaxSingleRowMenuSize == 0) {
                rowView = createRowView();
            }
            rowView.addView(createMenuItem(ms, ms.get(i), i));
        }
    }

    /**
     * 创建菜单行
     *
     * @return LinearLayout
     */
    private LinearLayout createRowView() {
        /**
         * 行View
         */
        LinearLayout rowView = new LinearLayout(getContext());
        rowView.setOrientation(HORIZONTAL);
        this.addView(rowView);
        return rowView;
    }

    /**
     * Create Menu Item View
     *
     * @param m DiscoveryColumnBean菜单的配置
     * @return View
     */
    private View createMenuItem(List<String> ms, String m, int position) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.view_material_image, this, false);
        //用来显示按钮名称
        if (m != null) {
            ImageView materialImg = ButterKnife.findById(itemView, R.id.materialImg);
            materialImg.setOnClickListener(this);
            materialImg.setTag(R.id.tag_first, ms);
            materialImg.setTag(R.id.tag_second, position);
            if (mViewGroupWidth > 0) {
                resetViewSize(materialImg);
            }
            JKImageLoader.getInstance().displayImage(mContext, UrlConstants.getBaseUrl()+m, materialImg);
        }
        return itemView;
    }

    /**
     * 重置View大小
     *
     * @param iv target ImageView
     */
    private void resetViewSize(ImageView iv) {
        ViewGroup.LayoutParams layoutParams = iv.getLayoutParams();
        if (layoutParams != null) {
            layoutParams.width = mViewGroupWidth;
            layoutParams.height = mViewGroupWidth;
        } else {
            layoutParams = new ViewGroup.LayoutParams(mViewGroupWidth, mViewGroupWidth);
        }
        iv.setLayoutParams(layoutParams);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.materialImg) {
            if (mPromoteClickListener != null) {
                mPromoteClickListener.onClick((ArrayList<String>)v.getTag(R.id.tag_first),
                        Integer.parseInt(v.getTag(R.id.tag_second).toString()));
            }
        }
    }

    /**
     * 设置单行多行的样式
     */
    public void setLineCountStyle(int maxCount, int count) {
        if (count < maxCount) {
            setMaxSingleRowMenuSize(count);
        } else {
            setMaxSingleRowMenuSize(maxCount);
        }
    }
}
